package com.bobo.base.utils.sign;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * 参数加密
 */
@RequiresApi(api = Build.VERSION_CODES.O)
public class DesUtil {
    /**
     * 加密解密方式
     */
    private final static String DES = "DES";
    /**
     * 秘钥
     */
    private final static String DEFAULTKEY = "asklgfasdgwgdsf";
    /**
     * 字符编码
     */
    private final static String ENCODE = "UTF-8";
    /**
     * 解码
     */
    private final static Base64.Decoder DECODER = Base64.getDecoder();
    /**
     * 编码
     */
    private final static Base64.Encoder ENCODER = Base64.getEncoder();

    /**
     * 加密 并用base64编码
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data) throws Exception {
//        return DESUtil.encrypt(data, DEFAULTKEY);
        byte[] bt = encrypt(data.getBytes(ENCODE), DEFAULTKEY.getBytes(ENCODE));
        String strs = ENCODER.encodeToString(bt);
        return strs;
    }

    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    /**
     * 解密 后并用base64解码
     * @param data
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data) throws IOException, Exception {
        if (data == null)
            return null;
        byte[] buf = DECODER.decode(data);
        byte[] bt = decrypt(buf, DEFAULTKEY.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    // 测试 加密解密
    public static void main(String[] args) throws Exception {
        String data = "a1c2c3c4c5c6";
        // System.err.println(encrypt(data, key));
        // System.err.println(decrypt(encrypt(data, key), key));
        System.out.println(encrypt(data));
        System.out.println(decrypt(encrypt(data)));
    }
}
